package com.bjtu.transactionclient.service;

import com.bjtu.transactionclient.api.api.IndexService;
import com.bjtu.transactionclient.api.api.JournalService;
import com.bjtu.transactionclient.api.constant.Constans;
import com.bjtu.transactionclient.api.utils.StringSimilarityUtil;
import com.bjtu.transactionclient.api.vo.request.SyncJournalRequest;
import com.bjtu.transactionclient.base.result.FailEnum;
import com.bjtu.transactionclient.base.result.Result;
import com.bjtu.transactionclient.base.result.SuccessEnum;
import com.bjtu.transactionclient.dao.AuthorIndexDao;
import com.bjtu.transactionclient.dao.JournalChineseDao;
import com.bjtu.transactionclient.dao.JournalEnglishDao;
import com.bjtu.transactionclient.dao.UserIndexDao;
import com.bjtu.transactionclient.domain.JournalChinese;
import com.bjtu.transactionclient.domain.UserIndexNoTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caiyuli
 * @date 2020/4/25 5:53 下午
 */

@Service
public class JournalServiceImpl implements JournalService {

    @Autowired
    private JournalChineseDao journalChineseDao;

    @Autowired
    private JournalEnglishDao journalEnglishDao;

    @Autowired
    private IndexService indexService;

    @Autowired
    private UserIndexDao userIndexDao;

    @Autowired
    private AuthorIndexDao authorIndexDao;


    @Override
    public Result syncJournal(SyncJournalRequest request) {

        Result result = null;
        if (request.getAuthorIndex().IndexCode.equals(Constans.IndexCode.JOURNAL_EN)){
            // 英文期刊
            result =  handleJournalCn(request);
        } else if (request.getAuthorIndex().IndexCode.equals(Constans.IndexCode.JOURNAL_CN)){
            // 中文期刊
            result =  handleJournalCn(request);
        }

        if (result == null){
            return Result.fail("不能处理该期刊类型");
        }
        return result;
    }


    private Result handleJournalCn(SyncJournalRequest request){
        Result result = null;
        UserIndexNoTable userIndexNoTable = userIndexDao.queryUserIndexNoByIndex(request.getUserIndexNoTable().getIndexNo());
        JournalChinese journalChinese = journalChineseDao.queryByIndexNo(request.getUserIndexNoTable().getIndexNo());
        if ( null == userIndexNoTable){
            // 索引号不同 需要进行全量的一个数据匹配，看看有没有已经存在但是索引号不一样的字段
            List<JournalChinese> journalChineseList = journalChineseDao.queryByIssn(request.getJournalChinese().getiSSN());
            if (journalChineseList.size() == 0){
                //  没有相同ISSN号的数据  直接插入
                insertJournalCn(request);
                return Result.success(null,SuccessEnum.JOURNAL_NEW);
            }
            //相同的ISSN 为同一期刊， 需要返回是其索引号让客户端端进行更新
            result = Result.success(journalChineseList.get(0).getIndexNo(), SuccessEnum.JOURNAL_INDEX_NOT_SAME_BUT_NATURE_SAME);

        }else {
            // 索引号相同  需要判断是是不是同一个本期刊
            // 这里做一个相关性检验
            Boolean isSame = isSameJournalCn(journalChinese, request.getJournalChinese());
            if (isSame == false){
                // 不是同一本期刊  返回 一个新的索引号
                String prefix = request.getAuthorIndex().getIndexNo().substring(0,request.getAuthorIndex().getIndexNo().length()-1) + '%';
                List<String> indexNoList =  userIndexDao.queryUserIndexNoLike(prefix);
                char lastLetter = LastIndexCodeLetter(indexNoList);

                if (lastLetter == 'A' && indexNoList.size() > 0){
                    //没有索引号分配了
                    return Result.fail(null,FailEnum.INDEXNO_FULL);
                }
                result = Result.success(lastLetter,SuccessEnum.INDEX_REDISTRIBUTION);
            }else {
                // 是同一本期刊
                result =  Result.success("同步成功", SuccessEnum.JOURNAL_INDEX_SAME_AND_NATURE_DIFFERENT);
            }
        }
        return result;
    }


    /**
     * 相识度检验
     * @param journalChinese1
     * @param journalChinese2
     * @return
     */

    private Boolean isSameJournalCn(JournalChinese journalChinese1, JournalChinese journalChinese2){
        if (!journalChinese1.getiSSN().equals(journalChinese2.getiSSN())){
            return false;
        }

        if (StringSimilarityUtil.cos(journalChinese1.getJournalName2(), journalChinese2.getJournalName2()) < Constans.SIMILARITY_THRESHOLD){
            return false;
        }
        return true;
    }

    private Result handleJournalEn(SyncJournalRequest request){
        //TODO: 逻辑与中文一致
        return null;

    }

    private void insertJournalCn(SyncJournalRequest request){
        userIndexDao.addUserIndexNoTable(request.getUserIndexNoTable());
        authorIndexDao.add(request.getAuthorIndex());
        journalChineseDao.add(request.getJournalChinese());
    }

    /**
     * 返回索引号下一个字母
     * @param indexNoList
     * @return
     */
    private Character LastIndexCodeLetter(List<String> indexNoList){
        char result = 'A';
        for (int i = 0; i < indexNoList.size(); i++){
            String str = indexNoList.get(i);
            char t = str.charAt(str.length()-1);
            if (t > result){
                result = t;
            }
        }
        if (result < 'Z'){
            return (char)(result+1);
        }
        return result;
    }

}
